package com.ssu.judea.palmssu.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;

import com.ssu.judea.palmssu.PalmActivity;
import com.ssu.judea.palmssu.R;
import com.ssu.judea.palmssu.adapter.PalmListAdapter;
import com.ssu.judea.palmssu.vo.MemberItem;

public class PalmListActivity extends PalmActivity implements OnClickListener,OnItemClickListener{
	private TextView textEmpty;
	private ListView listView;
	private ToggleButton buttonMatching;
	private ToggleButton buttonRecent;
	private ToggleButton buttonFollowers;
	private ArrayList<MemberItem> currentList;
	private PalmListAdapter adpater;
	private int currentButtonIdx = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		textEmpty = (TextView)findViewById(R.id.textEmptyList);
		listView = (ListView)findViewById(R.id.listView);
		listView.setOnItemClickListener(this);
		
		buttonMatching = (ToggleButton)findViewById(R.id.buttonMatching);
		buttonMatching.setOnClickListener(this);
		buttonRecent = (ToggleButton)findViewById(R.id.buttonRecent);
		buttonRecent.setOnClickListener(this);
		buttonFollowers = (ToggleButton)findViewById(R.id.buttonFollowers);
		buttonFollowers.setOnClickListener(this);
		
		currentList = getPalmApp().getMatchingList(getPalmApp().currentUser.getMemberseq());
		updateListView();
	}

	@Override
	public void onClick(View v) {
		buttonRecent.setChecked(false);
		buttonMatching.setChecked(false);
		buttonFollowers.setChecked(false);
		if(v==buttonRecent){
			if(currentButtonIdx!=2){
				currentList = getPalmApp().getRecentList(getPalmApp().currentUser.getMemberseq());
				updateListView();
				buttonRecent.setChecked(true);
			}
			currentButtonIdx = 2;
		}else if(v==buttonMatching){
			if(currentButtonIdx!=1){
				currentList = getPalmApp().getMatchingList(getPalmApp().currentUser.getMemberseq());
				updateListView();
				buttonMatching.setChecked(true);
			}
			currentButtonIdx = 1;
		}else if(v==buttonFollowers){
			if(currentButtonIdx!=3){
				currentList = getPalmApp().getFollowersList(getPalmApp().currentUser.getMemberseq());
				updateListView();
				buttonFollowers.setChecked(true);
			}
			currentButtonIdx = 3;
		}
	}
	
	private void updateListView(){
		if(currentList==null || currentList.size()<1){
			textEmpty.setVisibility(TextView.VISIBLE);
			listView.setAdapter(null);
		}else{
			textEmpty.setVisibility(TextView.GONE);
			adpater = new PalmListAdapter(this, R.layout.listrow, currentList);
			listView.setAdapter(adpater);
		}
		
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(this,DetailUserViewActivity.class);
		intent.putExtra("memberseq", currentList.get(position).getMemberseq());
		intent.putExtra("image", currentList.get(position).getImage());
		intent.putExtra("screenname", currentList.get(position).getScreenname());
		intent.putExtra("startdate", currentList.get(position).getStartdate());
		intent.putExtra("room", currentList.get(position).getRoom());
		intent.putExtra("seat", currentList.get(position).getSeat());
		intent.putExtra("comment", currentList.get(position).getComment());
		intent.putExtra("matchingrate", currentList.get(position).getMatchingrate());
		startActivity(intent);
	}
}
